package com.itcam.controller;

import com.itcam.pojo.Emp;
import com.itcam.pojo.PageBean;
import com.itcam.pojo.Result;
import com.itcam.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * 员工管理Controller
 */
@RestController
@Slf4j
public class EmpController {

    @Autowired
    private EmpService empService;


    @GetMapping("/emps")
    // @RequestParam的defaultValue属性可以设置默认值
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name,
                       Short gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("分页查询，参数：{},{},{},{},{},{}", page, pageSize, name, gender, begin, end);
        // 调用service分页查询
        PageBean pageBean = empService.page(page, pageSize, name, gender, begin, end);
        return Result.success(pageBean);
    }

    @DeleteMapping("/emps/{ids}") // 传递的是一个路径变量
    public Result delete(@PathVariable List<Integer> ids/*这里需要接收路径参数*/) {
        log.info("删除员工，参数：{}", ids);
        empService.delete(ids);
        return Result.success();
    }

    @PostMapping("/emps")
    public Result insert(@RequestBody Emp emp) {
        empService.insert(emp);
        return Result.success();
    }

    @GetMapping("/emps/{id}")
    public Result queryById(@PathVariable Integer id) {
        Emp emp = empService.queryById(id);
        return Result.success(emp);
    }

    @PutMapping("/emps")
    public Result update(@RequestBody Emp emp) {
        empService.update(emp);
        return Result.success();
    }
}
